//------------------------------------------------------------------------------
// <copyright file="Player.cpp" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

#include "pch.h"
#include "Player.h"
#include <memory>

namespace CppDemo
{

	Player::Player()
	{
		health = -1.0f;
	}

	Player::Player(RpcArchive^ archive)
	{
		health = archive->GetNamedFloatValue(L"health");
		name = archive->GetNamedStringValue(L"name");
        spells = archive->GetNamedStringCollection(L"spells");

        auto weaponCollection = archive->GetNamedArchiveCollection(L"weapons");
        weapons = ref new Platform::Array<Weapon^>(weaponCollection->Length);

        for (unsigned int i = 0; i < weapons->Length; ++i)
		{
			weapons[i] = ref new Weapon(weaponCollection[i]);
        }

        stature = ref new Stature(archive->GetNamedArchiveValue(L"stature"));
	}

	void Player::ToRpcArchive(RpcArchive^ result)
	{
		result->SetNamedFloatValue(L"health", this->health);
        result->SetNamedStringValue(L"name", this->name);
        result->SetNamedStringCollection(L"spells", this->spells);

		auto weapons = ref new Platform::Array<RpcArchive^>(this->weapons->Length);

        for (unsigned int i = 0; i < weapons->Length; ++i)
		{
            weapons[i] = this->weapons[i]->ToRpcArchive();
        }

        result->SetNamedArchiveCollection("weapons", weapons);

        result->SetNamedArchiveValue("stature", this->stature->ToRpcArchive());
	}

	Player^ Player::InitializeOutgoingPlayer()
	{
		auto p = ref new Player();
        p->name = L"Ragnor The Terrible";
        p->health = 0.34245f;
		p->spells = ref new Platform::Array<Platform::String^> { L"Fireball", L"Freeze", L"Hurricane", L"Force" };
		p->weapons = ref new Platform::Array<Weapon^>{ ref new Weapon(), ref new Weapon() };
        p->weapons[0]->damage = 13454;
        p->weapons[0]->modifier = -0.6;
        p->weapons[1]->damage = 923;
        p->weapons[1]->modifier = 2.4534;
		p->stature = ref new Stature();
        p->stature->height = 82;
        p->stature->weight = 63;
        return p;
	}

}